package org.example.business;


import org.example.manage.UserManage;
import org.example.model.User;

import javax.swing.*;

public class UserBusiness {
    public static User user;
    public static JMenuItem item;
    public static boolean passwordLogin(JPanel panel, String account, String password) {
        UserManage manage = new UserManage();
        boolean isCan = true;
        isCan = manage.hasUser(account);
        if (!isCan) {
            JOptionPane.showMessageDialog(panel, "账号不存在");
        }
        isCan = password.equals(manage.selectUser(account).password);
        if (!isCan) {
            JOptionPane.showMessageDialog(panel, "密码错误");
        }
        if (isCan) {
            user = manage.selectUser(account);
            if (user.condition.equals("已拉黑")) {
                JOptionPane.showMessageDialog(panel, "你的账号已被拉黑");
                isCan = false;
            }
        }
        manage.close();
        return isCan;
    }
    public static boolean mailBoxLogin(JPanel panel, String account, String code, String strCode) {
        UserManage manage = new UserManage();
        if(!manage.hasUser(account)){
            JOptionPane.showMessageDialog(panel, "账号不存在");
            return false;
        }
        if(strCode.equals(code)&& !strCode.isEmpty()){
            user = manage.selectUser(account);
            if (user.condition.equals("已拉黑")) {
                JOptionPane.showMessageDialog(panel, "你的账号已被拉黑");
                return false;
            }
            JOptionPane.showMessageDialog(panel, "登录成功");
        }
        else{
            JOptionPane.showMessageDialog(panel, "验证码错误");
            return false;
        }
        manage.close();
        return true;
    }
    public static boolean registration(JPanel panel, String account, String password, String confirmPassword, String code, String strCode) {
        boolean isCan = true;
        UserManage manage = new UserManage();
        if (account.isEmpty()||confirmPassword.isEmpty()||password.isEmpty()||code.isEmpty()) {
            JOptionPane.showMessageDialog(panel, "内容不能为空");
            return false;
        }
        isCan = !manage.hasUser(account);
        if (!isCan){
            JOptionPane.showMessageDialog(panel, "账号已存在");
            return false;
        }
        isCan = password.equals(confirmPassword);
        if (!isCan || password.isEmpty()){
            JOptionPane.showMessageDialog(panel, "密码错误");
            return false;
        }
        isCan = strCode.equals(code);
        if(!isCan || strCode.isEmpty()){
            JOptionPane.showMessageDialog(panel, "验证码错误");
            return false;
        }
        manage.close();
        return isCan;
    }
}
